package com.fuelwallet.view;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.text.Html;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TableRow.LayoutParams;
import android.widget.TextView;

import com.fuelwallet.R;
import com.fuelwallet.model.History;
import com.fuelwallet.model.HistoryHandle;

public class ShowHistoryActivity extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		HistoryHandle historyDatabase = new HistoryHandle(this);
		historyDatabase.open();
		/*
		 * historyDatabase.putHistory("aa", 12.5f, 100.5f);
		 * historyDatabase.putHistory("aa", 10.55f, 200.4f);
		 * historyDatabase.putHistory("bb", 3.6f, 30.7f);
		 * historyDatabase.putHistory("bb", 4.6f, 40.5f);
		 * historyDatabase.putHistory("bb", 5.5f, 60.7f);
		 */
		ArrayList<String> accountList = (ArrayList<String>) historyDatabase
				.getAccountNameList();
		// fuelpriceDatabase.close();
		setContentView(R.layout.showhistory);
		setTitle("Fuel Cost History");
		
		/* Find Tablelayout defined in main.xml */
		LinearLayout linear = (LinearLayout) findViewById(R.id.historylayout);
		TableLayout tl = (TableLayout) findViewById(R.id.historytable);
		TableRow rowTemp = (TableRow) findViewById(R.id.historyrow);
		for (int i = 0; i < accountList.size(); i++) {
			TableLayout table = new TableLayout(this);
			TextView tv = new TextView(this);
			TextView tvTemp = (TextView) findViewById(R.id.accountview);
			if (i == 0)
				tv = tvTemp;
			tv.setGravity(Gravity.CENTER);
			tv.setTextColor(tvTemp.getTextColors());
			tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, tvTemp.getTextSize());
			tv.setText(Html.fromHtml("<big>" + accountList.get(i)+"</big>"+"<small>(km/HKD)</small>"));
			
			if (i != 0)
				linear.addView(tv, new LinearLayout.LayoutParams(
						LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
			if (i == 0)
				table = tl;
			else {
				table.setLayoutParams(tl.getLayoutParams());
				table.setColumnShrinkable(0, true);
				table.setColumnStretchable(1, true);
				table.setColumnStretchable(2, true);
				TableRow dummyRow = new TableRow(this);
				TableRow tr = new TableRow(this);
				tr.setLayoutParams(rowTemp.getLayoutParams());
				tr.setBackgroundDrawable(rowTemp.getBackground());
				TextView dateView = new TextView(this);
				TextView distanceView = new TextView(this);
				TextView costView = new TextView(this);
				dateView.setText("Date");
				distanceView.setText("Distance");
				costView.setText("Cost");
				TextView dateTemp = (TextView) findViewById(R.id.dateview);
				TextView distanceTemp = (TextView) findViewById(R.id.distanceview);
				TextView costTemp = (TextView) findViewById(R.id.costview);
				dateView.setLayoutParams(dateTemp.getLayoutParams());
				distanceView.setLayoutParams(distanceTemp.getLayoutParams());
				costView.setLayoutParams(costTemp.getLayoutParams());
				dateView.setBackgroundDrawable(this.getResources().getDrawable(
						R.drawable.cell_shape));
				distanceView.setBackgroundDrawable(this.getResources()
						.getDrawable(R.drawable.cell_shape));
				costView.setBackgroundDrawable(this.getResources().getDrawable(
						R.drawable.cell_shape));
				dateView.setGravity(Gravity.CENTER);
				distanceView.setGravity(Gravity.CENTER);
				costView.setGravity(Gravity.CENTER);
				dateView.setTextSize(TypedValue.COMPLEX_UNIT_PX, dateTemp.getTextSize());
				distanceView.setTextSize(TypedValue.COMPLEX_UNIT_PX, distanceTemp.getTextSize());
				costView.setTextSize(TypedValue.COMPLEX_UNIT_PX, costTemp.getTextSize());
				dateView.setTextColor(dateTemp.getTextColors());
				distanceView.setTextColor(distanceTemp.getTextColors());
				costView.setTextColor(costTemp.getTextColors());
				dateView.setPadding(3, 3, 3, 3);
				distanceView.setPadding(3, 3, 3, 3);
				costView.setPadding(3, 3, 3, 3);
				dummyRow.addView(dateView);
				dummyRow.addView(distanceView);
				dummyRow.addView(costView);
				table.addView(dummyRow, new TableLayout.LayoutParams(
						LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
			}
			ArrayList<History> historyList = (ArrayList<History>) historyDatabase
					.getHistory(accountList.get(i));
			for (int j = 0; j < historyList.size(); j++) {
				History historyTemp = historyList.get(j);
				TableRow tr = new TableRow(this);
				tr.setLayoutParams(rowTemp.getLayoutParams());
				tr.setBackgroundDrawable(rowTemp.getBackground());
				TextView dateView = new TextView(this);
				TextView distanceView = new TextView(this);
				TextView costView = new TextView(this);
				dateView.setText(historyTemp.getDate());
				distanceView.setText(String.format("%.2f", historyTemp.getDistance()));
				costView.setText(String.format("%.2f", historyTemp.getCost()));
				TextView dateTemp = (TextView) findViewById(R.id.dateview);
				TextView distanceTemp = (TextView) findViewById(R.id.distanceview);
				TextView costTemp = (TextView) findViewById(R.id.costview);
				dateView.setLayoutParams(dateTemp.getLayoutParams());
				distanceView.setLayoutParams(distanceTemp.getLayoutParams());
				costView.setLayoutParams(costTemp.getLayoutParams());
				dateView.setBackgroundDrawable(this.getResources().getDrawable(
						R.drawable.childcell_shape));
				distanceView.setBackgroundDrawable(this.getResources()
						.getDrawable(R.drawable.childcell_shape));
				costView.setBackgroundDrawable(this.getResources().getDrawable(
						R.drawable.childcell_shape));
				dateView.setGravity(Gravity.CENTER);
				distanceView.setGravity(Gravity.CENTER);
				costView.setGravity(Gravity.CENTER);
				dateView.setTextColor(R.color.black);
				distanceView.setTextColor(R.color.black);
				costView.setTextColor(R.color.black);
				tr.addView(dateView);
				tr.addView(distanceView);
				tr.addView(costView);
				table.addView(tr, new TableLayout.LayoutParams(
						LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
			}
			LinearLayout.LayoutParams tableParams=new LinearLayout.LayoutParams(
					LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
			tableParams.setMargins(4,0,4,0);
			if (i != 0)
				linear.addView(table, tableParams);
			TextView dateTemp = (TextView) findViewById(R.id.dateview);
			View view = new View(this);
			// view.setLayoutParams(new LayoutParams(0, 2));
			view.setBackgroundColor(dateTemp.getCurrentTextColor());
			LinearLayout.LayoutParams textParams=new LinearLayout.LayoutParams(
					LayoutParams.FILL_PARENT, 4);
			textParams.setMargins(0,10,0,0);
			linear.addView(view, textParams);
		}
		historyDatabase.close();
		/* Add row to TableLayout. */
	}

	public void onBackPressed(){		
		super.onBackPressed();
		overridePendingTransition(android.R.anim.slide_in_left,android.R.anim.slide_out_right);		
	}
}